<?
/**
 * 관련 컴플라이언스 상세
 * by iamdobi
 */

class Page4 extends Controller {

	function Page4() {
		$this->initController();
		$this->getControllerInstance("Dao");
		$this->Dao->connect();
	}

	function execute() {
		$this->viewForm();
	}

	// 상세 내용 보여주기
	function viewForm() {
		// 이전 페이지에서 넘어온 체크박스 선택값 저장
		$this->ctrl_obj = $_REQUEST['ctrl_obj'];
		$this->comp_list = $_REQUEST['comp_list'];
		$ctrl_obj_num = sizeof($this->ctrl_obj);
		$comp_list_num = sizeof($this->comp_list);


		/*
		if($ctrl_obj_num < 1 || $comp_list_num < 1) {
			$this->ErrorBackMsg('최소한 한 개의 항목은 선택해 주십시오.');
			exit;
		}
		 */

		// kd_detail2 에서 자료를 조회하기 위해 div_3_id 를 구해온다
		$param = array();
		$query = 'select distinct(div_3_id) from kisa_detail where 1=1 and div_2_id in (? ';
		for($i=0; $i<$ctrl_obj_num; $i++) {
			if($i != 0) $query .= ',? ';
			$param[] = $this->ctrl_obj[$i];
		}
		$query .= ' ) ';

		$query .= ' and code in (? ';
		for($i=0; $i<$comp_list_num; $i++) {
			if($i != 0) $query .= ',? ';
			$param[] = $this->comp_list[$i];
		}
		$query .= ' ) ';

		$this->Dao->prepareStatement($query);
		$this->Dao->setParam($param);
		$this->Dao->executeStatement();

		$div_3_id_num = $this->Dao->getNumRows();
		if($div_3_id_num < 1) {
			$this->ErrorBackMsg('조회되는 상세 Compliance 값이 없습니다.');
			exit;
		}

		while($Rows = $this->Dao->getFetchArray()) {
			$div_3_id_list[] = $Rows['div_3_id'];
		}

		// kd_detail2 에서 상세 compliance 값을 가져오도록 한다
		$param = array();
		$query = 'select kd_detail2.*, div_3.ctrl_title, div_3.ctrl_content from kd_detail2, div_3 where 1=1 and kd_detail2.div_3_id in (? ';
		for($i=0; $i<$div_3_id_num; $i++) {
			if($i != 0) $query .= ',? ';
			$param[] = $div_3_id_list[$i];
		}
		$query .= ' ) ';

		$query .= ' and kd_detail2.code in (? ';
		for($i=0; $i<$comp_list_num; $i++) {
			if($i != 0) $query .= ',? ';
			$param[] = $this->comp_list[$i];
		}
		$query .= ' ) ';
		$query .= ' and kd_detail2.div_3_id = div_3.div_3_id ';
		$query .= ' order by kd_detail2.div_3_id asc ';
		$this->query_test = $query;

		$this->Dao->prepareStatement($query);
		$this->Dao->setParam($param);
		$this->Dao->executeStatement();

		// 가져온 리스트를 div_3_id를 기준으로 하여 object,code,detail을 하나의 row로 묶는다.
		$div_3_id_idx = 0;
		$div_3_id_idx2 = 0;
		// view 단에서는 kd_detail2를 가지고 DB 내용 조회
		while($Rows = $this->Dao->getFetchArray()) {
			$this->test[] = $Rows;
			if($this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['div_3_id'] != $Rows['div_3_id']) {
				$div_3_id_idx++;
				$div_3_id_idx2 = 0;
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['div_3_id'] = $Rows['div_3_id'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['code'] = $Rows['code'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['object'] = $Rows['object'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['detail'] = $Rows['detail'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['ctrl_title'] = $Rows['ctrl_title'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['ctrl_content'] = $Rows['ctrl_content'];
			} else {
				$div_3_id_idx2++;
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['div_3_id'] = $Rows['div_3_id'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['code'] = $Rows['code'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['object'] = $Rows['object'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['detail'] = $Rows['detail'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['ctrl_title'] = $Rows['ctrl_title'];
				$this->kd_detail2[$div_3_id_idx][$div_3_id_idx2]['ctrl_content'] = $Rows['ctrl_content'];
			}
		}

		$this->setLayout("page4");
		$this->dispatcher();
	}
}
?>
